package cn.bdqn.sptparent.web.controller;

import com.google.code.kaptcha.servlet.KaptchaExtend;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@Controller
public class CodeController  extends KaptchaExtend {
    /**
     * 获取验证码
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    @GetMapping(value = "/captcha.jpg")
    public void captcha(HttpServletRequest req, HttpServletResponse resp) throws
            ServletException, IOException {
        super.captcha(req, resp);
        // 方法一: 获取验证码,可存储到redis
        String key = super.getGeneratedKey(req);
        System.out.println("验证码："+key);
        // 方法二: session获取验证码(不推荐)
        String session_key = (String) req.getSession().getAttribute("KAPTCHA_SESSION_KEY");
        System.out.println("服务器端口的验证码："+session_key);
    }

}
